<template>
  <div>
    <cell
      is-link
      v-bind:title="titleName"
      :link="{name:'time-range-set',params:{timerIndex:this.itemdata.timerIndex.toString(),deviceBaseInfo:deviceBaseInfo},query:{productId:productId,deviceId:deviceId}}"
    >
      <div>{{time}}</div>

    </cell>
  </div>
</template>

<script>
import { getMYClient } from "../../js/client";
import { bus } from "../../js/client/bus";
// import { bytesToUint } from "../../js/hexUtil";
export default {
  name: "lighttimecell",
  props: {
    itemdata: {
      type: Object
    },
    deviceBaseInfo: {
      type: Object
    },
    deviceId: {
      type: String
    },
    productId: {
      type: String
    },
    enable:{
      type: Boolean
    }
  },
  data() {
    return {
      titleName: "定8时器",
      time: ""
    };
  },
  mounted: function() {
    let that = this;
    bus.$on("msg", function(productId, deviceId, message, topic) {
      
      let topicCompoments = topic.split("/");
      let pId = topicCompoments[1];
      let dId = topicCompoments[2];
      let compoment_2 = topicCompoments[3];
      let compoment_4 = topicCompoments[4];

      if (
        dId === String(that.deviceId) &&
        compoment_2 === "task"&&
        compoment_4 === String(that.itemdata.timerIndex)
      ) {
        //时间信息
        // let data = new Uint8Array(message);
       
        // var startTime = data.subarray(0, 4);
        // var endTime = data.subarray(4, 8);

        // var start_timestamp = bytesToUint(startTime);
        // var end_timestamp = bytesToUint(endTime);
   
        // var start_date = new Date(start_timestamp * 1000);
        // var end_date = new Date(end_timestamp * 1000);

        // console.log("开始时间 ---- ", start_date);
        // console.log("结束时间 ---- ", end_date);
        // // that.time = "" + start_date.toString() + end_date.toString();
        // // console.log("that.time", that.time);
        // // that.$forceUpdate();
        // // that.$set(that,'time',"" + start_date + end_date);
        //  that.time = String(start_date.getHours()) + ":" + String(start_date.getMinutes())




 let data = new Uint8Array(message);
        var startTime = data.subarray(0, 4);
        var endTime = data.subarray(4, 8);
        var start_timestamp = bytesToUint(startTime);
        var end_timestamp = bytesToUint(endTime);
        var mode = data.subarray(8, 9);
        var enable = data.subarray(9, 10);

        var modeString = ''
        if (mode[0] == 0x00) {
           modeString = ["关闭"][0];
        }else if (mode[0] == 0x01) {

          modeString = ["打开"][0];
        }else if (mode[0] == 0x02) {

          modeString = ["闪烁"][0];
        } else if (mode[0] == 0x03) {

          modeString = ["呼吸灯"][0];
        } 

        var enableString = ''
        if(enable[0] == 1){
          enableString = "开启"
        }else{
          enableString = "停用"
        }


        var start_date = new Date(start_timestamp * 1000);
       

        that.time = String(start_date.getHours()) + ":" + String(start_date.getMinutes()+" "+modeString + " " +enableString)



      }else{
        console.log("ID 不一致 !!!!!!!!!!!!!!!!!!!!!")
      }
    });

    
    this.titleName = "定时器" + String(this.itemdata.timerIndex);
    this.time = ''
    var topic =
      "/" +
      String(this.productId) +
      "/" +
      String(this.deviceId) +
      "/task/" +
      String(this.itemdata.timerIndex) +
      "/get";
    getMYClient().mqtt_client.publish(topic, "hahaha",2);




  }
};
</script>

<style scoped>
</style>


